﻿@{
    @*Get userid*@
    var uid = KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString();
    Guid? user_id;
    if (uid != "")
    {
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
    else
    {
        KimPhongCRM.Controllers.BaseController bcontrol = new KimPhongCRM.Controllers.BaseController();
        string un = User.Identity.Name;
        bcontrol.ressUserID(un);
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
    @*Get userid*@    
    var listUser = ViewData["listUser"] as List<KimPhongCRM.Models.sp_Select_Combobox_User_Result>;
    var grid = Html.DevExpress().GridView(settings =>
    {
        settings.Name = "ListLoaiThe";
        settings.CallbackRouteValues = new { Controller = "LoaiThe", Action = "ListLoaiThePartial" };

        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "LoaiThe", Action = "ListLoaiThePartialAddNew" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "LoaiThe", Action = "ListLoaiThePartialUpdate" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "LoaiThe", Action = "ListLoaiThePartialDelete" };

        settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
        settings.SettingsPopup.EditForm.AllowResize = true;
        settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.Width = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Height = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Modal = true;
        settings.SettingsBehavior.ConfirmDelete = true;
        
        settings.CommandColumn.ButtonType = System.Web.UI.WebControls.ButtonType.Image;
        settings.CommandColumn.SetColVisibleIndex(14);
        settings.CommandColumn.SetHeaderCaptionTemplateContent("Tùy chọn");
        settings.CommandColumn.Width = 200;

        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.NewButton.Image.Url = @Url.Content("~/Content/img/icon-24-new.png");
        settings.CommandColumn.NewButton.Image.ToolTip = "Thêm mới";
        settings.CommandColumn.NewButton.Image.AlternateText = "Thêm";

        settings.CommandColumn.EditButton.Visible = true;
        settings.CommandColumn.EditButton.Image.Url = @Url.Content("~/Content/img/icon-24-edit.png");
        settings.CommandColumn.EditButton.Image.ToolTip = "Thay đổi";
        settings.CommandColumn.EditButton.Image.AlternateText = "Sửa";

        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.DeleteButton.Image.Url = @Url.Content("~/Content/img/icon-24-delete.png");
        settings.CommandColumn.DeleteButton.Image.ToolTip = "Xóa";
        settings.CommandColumn.DeleteButton.Image.AlternateText = "Xóa";

        settings.CommandColumn.UpdateButton.Visible = true;
        settings.CommandColumn.UpdateButton.Image.Url = @Url.Content("~/Content/img/icon-24-ok.png");
        settings.CommandColumn.UpdateButton.Image.ToolTip = "Lưu lại";
        settings.CommandColumn.UpdateButton.Image.AlternateText = "Lưu";

        settings.CommandColumn.CancelButton.Visible = true;
        settings.CommandColumn.CancelButton.Image.Url = @Url.Content("~/Content/img/icon-24-cancel.png");
        settings.CommandColumn.CancelButton.Image.ToolTip = "Hủy";
        settings.CommandColumn.CancelButton.Image.AlternateText = "Hủy";

        settings.CommandColumn.Visible = true;

        settings.CommandColumn.Visible = true;
        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.EditButton.Visible = true;

        settings.KeyFieldName = "maThe";

        settings.SettingsPager.Visible = true;
        settings.Settings.ShowGroupPanel = true;
        settings.Settings.ShowFilterRow = true;
        settings.SettingsBehavior.AllowSelectByRowClick = true;

        settings.Columns.Add(column =>
        {
            column.FieldName = "maThe";
            column.Caption = "Mã thẻ";
            column.Index = 1;
            column.Width = 100;
            column.ReadOnly = true;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "tenThe";
            column.Caption = "Tên thẻ";
            column.Index = 2;
            column.Width = 200;

        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngayNhap";
            column.Caption = "Ngày nhập";
            column.Index = 3;
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            var dateProperties = column.PropertiesEdit as DateEditProperties;
            dateProperties.UseMaskBehavior = true;
            dateProperties.EditFormat = EditFormat.DateTime;
            dateProperties.EditFormatString = "g";
            column.ReadOnly = true;
            column.Width = 200;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "nguoiNhap";
            column.Caption = "Người nhập";
            column.Index = 4;
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listUser)
            {
                comboBoxProperties.Items.Add(value.UserName, value.UserId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Width = 200;

        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "ngaySuaCuoi";
            column.Caption = "Ngày sửa cuối";
            column.Index = 5;
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            var dateProperties = column.PropertiesEdit as DateEditProperties;
            dateProperties.UseMaskBehavior = true;
            dateProperties.EditFormat = EditFormat.DateTime;
            dateProperties.EditFormatString = "g";
            column.ReadOnly = true;
            column.Width = 200;

        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "nguoiSuaCuoi";
            column.Caption = "Người sửa cuối";
            column.Index = 6;
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listUser)
            {
                comboBoxProperties.Items.Add(value.UserName, value.UserId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Width = 200;

        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "mauThe";
            column.Caption = "Màu thẻ";
            column.Index = 7;
            column.ColumnType = MVCxGridViewColumnType.ColorEdit;
            column.Width = 60;

        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "isActive";
            column.Caption = "Active";
            column.Index = 8;
            column.Width = 200;
            column.ColumnType = MVCxGridViewColumnType.CheckBox;
            column.SetDataItemTemplateContent(c =>
            {
                bool actived = (bool)DataBinder.Eval(c.DataItem, "isActive");
                object mathe_obj = DataBinder.Eval(c.DataItem, "maThe");
                string updateLink = Url.Action("ToogleActive", "Base", new { keyvalue = mathe_obj, active = !actived, tablename ="tblLoaiThe",keyname = "mathe" });
                ViewContext.Writer.Write(
                    "<a href=\"" + updateLink + "\" id=\"id_button_active_" + mathe_obj + "\" class=\"button btn-active\"><i class=\"font-icon-" + (actived ? "check" : "check-empty") + "\"></i></a>"
                );
            });

        });
        settings.InitNewRow = (s, e) =>
        {
            e.NewValues["maThe"] = "0";
            e.NewValues["ngaySuaCuoi"] = System.DateTime.Now;
            e.NewValues["ngayNhap"] = System.DateTime.Now;
            e.NewValues["isActive"] = true;
            e.NewValues["nguoiNhap"] = user_id;
            e.NewValues["nguoiSuaCuoi"] = user_id;
        };
        //edit init
        settings.CellEditorInitialize = (sender, e) =>
        {
            //ASPxGridView gridView = sender as ASPxGridView;
            //if (sender == null || !gridView.IsNewRowEditing)
            //return;
            if (e.Column.FieldName == "nguoiSuaCuoi")
            {
                e.Editor.Value = user_id;
            }
            if (e.Column.FieldName == "ngaySuaCuoi")
            {
                e.Editor.Value = System.DateTime.Now;
            }
        };
    });
    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}
@grid.Bind(Model).GetHtml()